package se.lth.eit.eit060.proj2.initiation;

import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.KeyStore;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;

public class Utility {
	
	public static final int PORT = 8082;
	public static final String SPLIT = "##123!SPLIT!321##";//"#€$£&"; //Windows-Linux konvertering av dessa tecken blev cp s� fick byta.
	public static final String DELETE = "Delete record";
	public static final String READ = "Read record";
	public static final String LIST = "List records";
	public static final String CREATE = "Create record";
	public static final String UPDATE = "Update record";
	public static final String LOGIN = "getlogininfo";
	public static final String WARNING = "bigwarning";
	public static final String FULLWARNING = WARNING+SPLIT+"ERROR";
	public static final String SUCCESS = "actionsuccess";
	
	public static final String NURSE ="Nurse";
	public static final String DOCTOR = "Doctor";
	public static final String Agent = "Agent";
	public static final String Patient ="Patient";
	public static final String PERSONFILE="Persons.txt";
	
	public static SSLContext generate(String keystore, char[] password){
		System.setProperty("javax.net.ssl.trustStore", keystore);
		SSLContext ctx = null;
		try {
			try {
				KeyManagerFactory kmf;
				KeyStore ks;
				ctx = SSLContext.getInstance("TLS");
				kmf = KeyManagerFactory.getInstance("SunX509");
				ks = KeyStore.getInstance("JKS");
				ks.load(new FileInputStream(keystore), password);
				kmf.init(ks, password);
				ctx.init(kmf.getKeyManagers(), null, null);
			} catch (Exception e) {
				throw new IOException(e.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ctx;
	}
	public static String hashCode(String pwd){
		String hashword = null;
			MessageDigest sha;
			try {
				sha = MessageDigest.getInstance("SHA");
			sha.update(pwd.getBytes());
			BigInteger hash = new BigInteger(1,sha.digest());
			return hash.toString(16);
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			}
			return null;
	}
}
